package com.jhgsys.internal.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jhgsys.internal.base.dto.BaseAreSchedulingDto;
import com.jhgsys.internal.base.dto.BaseAreaSchedulingDto;
import com.jhgsys.internal.base.dto.BaseAreaSchedulingInfoQuery;
import com.jhgsys.internal.base.entity.BaseArea;
import com.jhgsys.internal.base.entity.BaseAreaScheduling;
import com.jhgsys.internal.base.mapper.BaseAreaSchedulingMapper;
import com.jhgsys.internal.base.service.IBaseAreaSchedulingService;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.BeanHelper;
import com.jhgsys.internal.common.utils.DateUtil;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.common.utils.SortUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 区域排班 Service实现
 *
 * @author admin
 * @date 2024-07-31 14:35:45
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class BaseAreaSchedulingServiceImpl extends ServiceImpl<BaseAreaSchedulingMapper, BaseAreaScheduling> implements IBaseAreaSchedulingService {

    /**
     * 查询（分页）
     * @author admin
     * @date 2024-07-31 14:35:45
     * @param request QueryRequest
     * @param baseAreaScheduling baseAreaScheduling
     * @return IPage<BaseAreaScheduling>
    */
    @Override
    public IPage<BaseAreaScheduling> findBaseAreaSchedulingPage(QueryRequest request, BaseAreaScheduling baseAreaScheduling) {
        LambdaQueryWrapper<BaseAreaScheduling> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseAreaScheduling::getDeletedFlg, 0);
        // TODO 设置查询条件
        if(StringUtils.isNotBlank(baseAreaScheduling.getAreaId())){
            queryWrapper.eq(BaseAreaScheduling::getAreaId,baseAreaScheduling.getAreaId());
        }
        if(baseAreaScheduling.getSchedulingDate() != null){
            queryWrapper.eq(BaseAreaScheduling::getSchedulingDate,baseAreaScheduling.getSchedulingDate());
        }

        Page<BaseAreaScheduling> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        SortUtil.handlePageSort(request, page, "schedulingDate", FebsConstant.ORDER_DESC, true);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询所有
     *
     * @param scheduling
     * @return
     */
    @Override
    public List<BaseAreaSchedulingDto> findBaseAreaSchedulingList(BaseAreaSchedulingDto scheduling) {
        List<BaseAreaSchedulingDto> dtoList = new ArrayList<>();
        LambdaQueryWrapper<BaseAreaScheduling> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseAreaScheduling::getDeletedFlg, 0);
        // TODO 设置查询条件
        if(StringUtils.isNotBlank(scheduling.getAreaId())){
            queryWrapper.eq(BaseAreaScheduling::getAreaId,scheduling.getAreaId());
        }
        if(StringUtils.isNotBlank(scheduling.getStartDate()) && StringUtils.isNotBlank(scheduling.getEndDate())){
            queryWrapper.between(BaseAreaScheduling::getSchedulingDate,DateUtil.parseDate(scheduling.getStartDate(),DateUtil.YYYY_MM_DD_PATTERN),DateUtil.parseDate(scheduling.getEndDate(),DateUtil.YYYY_MM_DD_PATTERN));
        }
        if(StringUtils.isBlank(scheduling.getStartDate()) && StringUtils.isNotBlank(scheduling.getEndDate())){
            queryWrapper.le(BaseAreaScheduling::getSchedulingDate,DateUtil.parseDate(scheduling.getEndDate(),DateUtil.YYYY_MM_DD_PATTERN));
        }
        if(StringUtils.isNotBlank(scheduling.getStartDate()) && StringUtils.isBlank(scheduling.getEndDate())){
            queryWrapper.ge(BaseAreaScheduling::getSchedulingDate,DateUtil.parseDate(scheduling.getStartDate(),DateUtil.YYYY_MM_DD_PATTERN));
        }

        List<BaseAreaScheduling> list = this.baseMapper.selectList(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            dtoList = BeanHelper.copyWithCollection(list,BaseAreaSchedulingDto.class);
            dtoList.forEach(d->{
                d.setDateFlg(DateUtil.compareWithNow(d.getSchedulingDate()));
            });
        }
        return dtoList;
    }


    /**
    * 根据ID查询
    * @author admin
    * @date 2024-07-31 14:35:45
    * @param id
    * @return BaseAreaScheduling
    */
    @Override
    public BaseAreaScheduling findBaseAreaSchedulingById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author admin
     * @date 2024-07-31 14:35:45
     * @param baseAreaScheduling baseAreaScheduling
     */
    @Override
    @Transactional
    public void createBaseAreaScheduling(BaseAreaScheduling baseAreaScheduling) {
        baseAreaScheduling.setCreatedTime(new Date());
        this.save(baseAreaScheduling);
    }


    /**
     * 修改
     * @author admin
     * @date 2024-07-31 14:35:45
     * @param baseAreaScheduling baseAreaScheduling
     */
    @Override
    @Transactional
    public void updateBaseAreaScheduling(BaseAreaScheduling baseAreaScheduling) {
        baseAreaScheduling.setUpdatedTime(new Date());
        this.saveOrUpdate(baseAreaScheduling);
    }

    /**
     * 逻辑删除
     * @author admin
     * @date 2024-07-31 14:35:45
     * @param baseAreaScheduling baseAreaScheduling
     * @param baseAreaSchedulingIds baseAreaSchedulingIds
     */
    @Override
    @Transactional
    public void deleteBaseAreaScheduling(BaseAreaScheduling baseAreaScheduling,String baseAreaSchedulingIds) {
        List<String> list = Arrays.asList(baseAreaSchedulingIds.split(StringPool.COMMA));
        this.baseMapper.update(baseAreaScheduling,new LambdaQueryWrapper<BaseAreaScheduling>().in(BaseAreaScheduling::getAreaSchedulingId,list));
	}

    /**
     * 批量逻辑删除
     *
     * @param areaId areaId
     * @author zhangwei
     * @date 2024-09-10 09:59:32
     */
    @Override
    public void batchDeleteBaseAreaScheduling(String areaId) {
        LambdaQueryWrapper<BaseAreaScheduling> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseAreaScheduling::getDeletedFlg, 0);
        // TODO 设置查询条件
        queryWrapper.eq(BaseAreaScheduling::getAreaId,areaId);
        String now = DateUtil.getDateFormat(new Date(),DateUtil.YYYY_MM_DD_PATTERN);
        queryWrapper.ge(BaseAreaScheduling::getSchedulingDate,DateUtil.parseDate(now,DateUtil.YYYY_MM_DD_PATTERN));
        List<BaseAreaScheduling> list = this.baseMapper.selectList(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            String ids = list.stream().map(BaseAreaScheduling::getAreaSchedulingId).collect(Collectors.joining(","));
            list.forEach(i->{
                this.deleteBaseAreaScheduling(i,ids);
            });
        }
    }

    /**
    * 物理删除
    * @author admin
    * @date 2024-07-31 14:35:45
    * @param baseAreaSchedulingIds baseAreaSchedulingIds
    */
    @Override
    @Transactional
    public void physicallyDeleteBaseAreaScheduling(String baseAreaSchedulingIds) {
        List<String> list = Arrays.asList(baseAreaSchedulingIds.split(StringPool.COMMA));
        LambdaQueryWrapper<BaseAreaScheduling> wapper = new LambdaQueryWrapper<>();
        wapper.in(BaseAreaScheduling::getAreaSchedulingId,list);
        this.remove(wapper);
    }

    @Override
    public List<String> selectDate(BaseAreaSchedulingInfoQuery query) {
        return this.baseMapper.selectDate(query);
    }

    @Override
    public List<BaseAreaScheduling> selectNewestSchedule(String areaId) {
        return this.baseMapper.selectNewestSchedule(areaId);
    }


}
